<?php
class Model_Table extends Zend_Db_Table_Abstract {
	protected $DataAp;
	public function __construct() {
		$this->DataAp = Zend_Registry::get ( "db" );
	}
	public function getFetObj($table, $settings, $options) {
		$result = array ();
		// Lay tong so ban ghi
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM $table" );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];
		// Thuc hien cau lenh truy xuat du lieu
		//la phong		
		foreach($options as $key => $value) {
			$$key = $value;
		}
		if($table == 'user'){
			$query = $this->DataAp->query ( "SELECT *
					FROM $table where roles != 'Admin' order by  $sort $order LIMIT $offset, $rows" );
			}
		else
		$query = $this->DataAp->query ( "SELECT *
					FROM $table order by  $sort $order LIMIT $offset, $rows" );			
		$result ['rows'] = $query->fetchAll ();
		
		return $result;
	}
	
    public function getFetcatalogObj($table, $settings, $options,$category) {
		$result = array ();
		// Lay tong so ban ghi
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM $table" );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];
		// Thuc hien cau lenh truy xuat du lieu
		//la phong		
		foreach($options as $key => $value) {
			$$key = $value;
		}
			$query = $this->DataAp->query ( "SELECT *
					FROM $table where type = '$category' order by  $sort $order LIMIT $offset, $rows" );
		$result ['rows'] = $query->fetchAll ();
		
		return $result;
	}
    
	public function addObj($table, $data) {
		return $this->DataAp->insert($table, $data);
	}
	
	public function updateObj($table, $data, $id) {
		return $this->DataAp->update($table, $data, 'id='.$id);
	}
	
	public function del($table,$id) {
		$Id = $this->DataAp->delete ( $table, 'id = ' . $id );
		return $Id;
	}
	
	public function getFetObjByMadn($madn) {
		$query = $this->DataAp->query ( " SELECT id, name  FROM catalog WHERE id = '$madn'" );
		return $query->fetchAll ();
	}
	
	public function getComborole() {
		$query = $this->DataAp->query("SELECT id, name, code  FROM role");
		$result = array();
		$result = $query->fetchAll();
		return $result;
	}
    public function getParenttree() {
		$query = $this->DataAp->query("SELECT id, name as text FROM catalog where isCat = 1");
		$result = array();
		$result = $query->fetchAll();
		return $result;
	}
    
    
    public function getItems($id,$table,$settings) {
		$result = array();        
		$query = $this->DataAp->query ( "SELECT * FROM catalog where parentId=$id " );
		 $data = $query->fetchAll ();
		 foreach($data as &$row)
		 {
		  if($row['isCat'] > 0){
			$row['state'] =  'closed';}
          else
		 	$row['state'] =  'open';
		 }
		return $data;
	}
    
	public function select($table, $id) {
		$query = $this->DataAp->query ( "SELECT * FROM `$table` where id=$id limit 0, 1" );
		 $data = $query->fetchAll ();
		 if($data) {
			return $data[0];
		 }
		 return false;
	}
	public function selectCond($table, $cond) {
		$query = $this->DataAp->query ( "SELECT * FROM `$table` where $cond limit 0, 1" );
		 $data = $query->fetchAll ();
		 if($data) {
			return $data[0];
		 }
		 return false;
	}
}